//
//  FR_History_Identity_ViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2017/2/14.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_History_Identity_ViewController.h"
#import "FR_EmployeeHistoryViewController.h"
#import "FR_HistoryViewController.h"
#import "FR_StudentRecordList_ViewController.h"


@interface FR_History_Identity_ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *IdentityTable;
@property (nonatomic, strong) NSMutableArray *titleArr;

@end

@implementation FR_History_Identity_ViewController

- (id)init
{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)loadView {
    
    [super loadView];
    self.view.backgroundColor = colorFromHex(@"#F5F8FA");
    
    self.navBar.nav_titleLabel.text = @"记录";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleArr = [[NSMutableArray alloc]init];
    
    
    [self.view addSubview:self.IdentityTable];
    
    //[self loadAllclasses];
    [self Java_getClasses];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)Java_getClasses {
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    [paramDic setObject:USERID forKey:@"teacherid"];
    [paramDic setObject:SCHOOLID forKey:@"scid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.schoolManage > 0?@"1":@"0" forKey:@"type"];
    
    [Utils Java_get:@"api/getTeacherClass" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if(arr.count > 0){ //有班级
            
            [self.titleArr addObject:@"班级记录"];
        }
        if ([SYAccountTool ShareAccount].account.schoolManage > 0) {
            [self.titleArr addObject:@"员工记录"];
        }
        
        [self.titleArr addObject:@"个人记录"];
        
        [self.IdentityTable reloadData];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
        
        if ([SYAccountTool ShareAccount].account.schoolManage > 0) {
            [self.titleArr addObject:@"员工记录"];
        }
        
        [self.titleArr addObject:@"个人记录"];
        
        [self.IdentityTable reloadData];
        
    }];
}

- (void)loadAllclasses {
    
    NSMutableDictionary * paramDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [paramDic setObject:USERID forKey:@"teacherid"];
    [paramDic setObject:SCHOOLID forKey:@"scid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.schoolManage > 0?@"1":@"0" forKey:@"type"];
    
    [Utils Java_get:@"api/getTeacherClass" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if(arr.count > 0){ //有班级
            
            [self.titleArr addObject:@"班级记录"];
        }
        if ([SYAccountTool ShareAccount].account.schoolManage > 0) {
            [self.titleArr addObject:@"员工记录"];
        }
        
        [self.titleArr addObject:@"个人记录"];
        
        [self.IdentityTable reloadData];
    } failure:^(NSInteger recode, NSError *error) {
        if ([SYAccountTool ShareAccount].account.schoolManage > 0) {
            [self.titleArr addObject:@"员工记录"];
        }
        
        [self.titleArr addObject:@"个人记录"];
        
        [self.IdentityTable reloadData];
    }];
    
}

#pragma mark - TableViewDateSource & TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellIdentifier"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    
    cell.textLabel.text = [self.titleArr objectAtIndex:indexPath.section];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 10)];
    [footView setBackgroundColor:colorFromHex(@"#F5F8FA")];
    
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([[self.titleArr objectAtIndex:indexPath.section] isEqualToString:@"班级记录"]) {
        
        FR_HistoryViewController *vc = [[FR_HistoryViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([[self.titleArr objectAtIndex:indexPath.section] isEqualToString:@"员工记录"]) {
        
        FR_EmployeeHistoryViewController *vc = [[FR_EmployeeHistoryViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([[self.titleArr objectAtIndex:indexPath.section] isEqualToString:@"个人记录"]) {
        
        FR_StudentRecordList_ViewController *vc = [[FR_StudentRecordList_ViewController alloc]init];
        vc.faceuserid = [NSString stringWithFormat:@"%lld",[SYAccountTool ShareAccount].account.faceuserid];
        vc.userid = USERID;
        vc.recordType = 1;
        vc.scid = @"0";
        [self.navigationController pushViewController:vc animated:YES];
        

    }
    
}

#pragma mark - Get

- (UITableView *)IdentityTable {
    
    if (_IdentityTable == nil) {
        _IdentityTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight + 10, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight-SafeAreaBottomHeight -10)];
        _IdentityTable.delegate = self;
        _IdentityTable.dataSource = self;
        _IdentityTable.rowHeight = 45;
        _IdentityTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_IdentityTable setBackgroundColor:colorFromHex(@"#F5F8FA")];
    }
    return _IdentityTable;
}


@end
